﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dbPoll_Application.Models;
using dbPoll_Application.Controllers;

namespace dbPoll_Application.ViewModels
{
    public class PollReportViewModel
    {
        public int pollID { get; set; }
        public String name { get; set; }
        public String uri { get; set; }
        public IList<Question> questions { get; set; }
        public IDictionary<Question, IDictionary<String, int>> responses { get; set; }
        public IList<Participant> participants { get; set; }
        public IList<Poll> allPolls { get; set; }
        public IDictionary<Poll, IList<User>> creatorList { get; set; }
        public IDictionary<Poll, int> attendanceList { get; set; }
        public IList<User> userList { get; set; }
        public IDictionary<String, int> roleList { get; set; }
        public IList<String> correct { get; set; }
        public Poll poll { get; set; }
        public ParticipantField field { get; set; }
        public bool include { get; set; }
        public String[] tick { get; set; }
        public Entity root { get; set; }
        public IList<Entity> selectedEntities { get; set; }
        public IDictionary<Question, IDictionary<String, IDictionary<int, int>>> filteredResponses { get; set; }

        public PollReportViewModel() { uri = "../../resources/MyFirstPDF" + Helpers.CodeFile1.FILE + ".pdf"; }

        public PollReportViewModel(int pollID, String name)
        {
            this.pollID = pollID;
            this.name = name;
            this.poll = PollRepository.GetPollByID(pollID);
            this.include = true;
            uri = "../../resources/MyFirstPDF" + Helpers.CodeFile1.FILE + ".pdf";
            this.field = new ParticipantField();
            this.selectedEntities = new List<Entity>();
        }
        public PollReportViewModel(int pollID, String name, IList<Question> questions, IDictionary<Question, IDictionary<String, int>> responses, ParticipantField field, bool include, String[] tick)
        {
            this.pollID = pollID;
            this.name = name;
            this.poll = PollRepository.GetPollByID(pollID);
            this.field = field;
            this.include = include;
            this.tick = tick;
            this.questions = questions;
            this.responses = responses;
            this.poll = PollRepository.GetPollByID(pollID);
            this.selectedEntities = new List<Entity>();
        }

        public PollReportViewModel(int pollID, String name, IList<Question> questions, IDictionary<Question, IDictionary<String, int>> responses)
        {
            this.pollID = pollID;
            this.name = name;
            this.questions = questions;
            this.responses = responses;
            this.poll = PollRepository.GetPollByID(pollID);
            this.include = true;
            this.selectedEntities = new List<Entity>();
        }

        public PollReportViewModel(int pollID, String name, IDictionary<Question, IDictionary<String, int>> responses, IList<String> correct)
        {
            this.pollID = pollID;
            this.name = name;
            this.responses = responses;
            this.correct = correct;
            this.include = true;
            this.poll = PollRepository.GetPollByID(pollID);
            this.field = new ParticipantField();
            this.selectedEntities = new List<Entity>();
        }

        public PollReportViewModel(int pollID, String name, IList<Participant> participants)
        {
            this.pollID = pollID;
            this.name = name;
            this.participants = participants;
            this.include = true;
            this.poll = PollRepository.GetPollByID(pollID);
            this.selectedEntities = new List<Entity>();
        }

        public PollReportViewModel(IList<Poll> allPolls, 
                IDictionary<Poll, IList<User>> creatorList, IDictionary<Poll, int> attendanceList)
        {
            this.allPolls = allPolls;
            this.creatorList = creatorList;
            this.attendanceList = attendanceList;
            this.include = true;
            this.selectedEntities = new List<Entity>();
        }

        public PollReportViewModel(IList<User> userList)
        {
            this.userList = userList;
            this.include = true;
            this.selectedEntities = new List<Entity>();
            this.uri = "../../resources/MyFirstPDF" + Helpers.CodeFile1.FILE + ".pdf";
        }

        public PollReportViewModel(IDictionary<String, int> roleList)
        {
            this.roleList = roleList;
            this.include = true;
            this.selectedEntities = new List<Entity>();
        }

        public String choices(int fieldID, String[] items, int pollID)
        {
            PollController pollController = new PollController();
            
            return pollController.demographicfield(fieldID,pollID,items);
        }
        
        public string participated(Participant participant)
        {
            return Math.Round((participating(participant) * 100.00 / poll.questions.Count()),2)+"%";
        }

        public int participating(Participant participant)
        {
            IList<Question> shortre = poll.questions.Where(m => m.type == QuestionType.Alphanumeric || m.type == QuestionType.Numeric).ToList();
            IList<Question> multip = poll.questions.Where(m => m.type != QuestionType.Alphanumeric && m.type != QuestionType.Numeric).ToList();
            IList<ShortResponseQuestion> shortr = new List<ShortResponseQuestion>();
            IList<MultipleChoiceQuestion> multi = new List<MultipleChoiceQuestion>();
            foreach (Question shorty in shortre) shortr.Add(QuestionRepository.GetQuestionByID<ShortResponseQuestion>(shorty.questionID));
            foreach (Question multipl in multip) multi.Add(QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(multipl.questionID));
            int total = shortr.Count() + multi.Count();
            int answered = 0;
            foreach (ShortResponseQuestion ques in shortr)
                foreach (ShortResponse res in ques.responses) if (res.participant == participant) answered++;
            foreach (MultipleChoiceQuestion ques in multi)
            {
                bool breaking = false;
                foreach (QuestionOption option in ques.options)
                {
                    foreach (OptionResponse res in option.responses) if (res.participant == participant) { answered++; breaking = true; break; }
                    if (breaking) break;
                }
            }
            return answered;
        }
        public String points(Participant participant)
        {
            double points=0;
            double total = 0;
            IList<Question> multip = poll.questions.Where(m => m.type != QuestionType.Alphanumeric && m.type != QuestionType.Numeric).ToList();
            IList<MultipleChoiceQuestion> multi = new List<MultipleChoiceQuestion>();
            foreach (Question multipl in multip) multi.Add(QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(multipl.questionID));
            foreach (MultipleChoiceQuestion ques in multi)
            {
                foreach (QuestionOption option in ques.options)
                {
                    total += option.pointsCurrent;
                    foreach (OptionResponse res in option.responses) if (res.participant == participant) points += option.pointsCurrent;
                }
            }
            return points + "/" + total;
        }
        public Double onlypoints(Participant participant)
        {
            double points = 0;
            IList<Question> multip = poll.questions.Where(m => m.type != QuestionType.Alphanumeric && m.type != QuestionType.Numeric).ToList();
            IList<MultipleChoiceQuestion> multi = new List<MultipleChoiceQuestion>();
            foreach (Question multipl in multip) multi.Add(QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(multipl.questionID));
            foreach (MultipleChoiceQuestion ques in multi)
            {
                foreach (QuestionOption option in ques.options)
                {
                    foreach (OptionResponse res in option.responses) if (res.participant == participant) points += option.pointsCurrent;
                }
            }
            return points;
        }
        public double onlytotal()
        {
            double total = 0;
            IList<Question> multip = poll.questions.Where(m => m.type != QuestionType.Alphanumeric && m.type != QuestionType.Numeric).ToList();
            IList<MultipleChoiceQuestion> multi = new List<MultipleChoiceQuestion>();
            foreach (Question multipl in multip) multi.Add(QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(multipl.questionID));
            foreach (MultipleChoiceQuestion ques in multi)
            {
                foreach (QuestionOption option in ques.options)
                {
                    total += option.pointsCurrent;
                }
            }
            return total;
        }
    }
}
